package cn.jeeweb.common.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseInterceptor implements HandlerInterceptor {

    /**
     * 拦截前处理
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        long startTime = System.currentTimeMillis();
        request.setAttribute("xjs_startTime", startTime);
        return true;
    }

    /**
     * 拦截后处理
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
//        long startTime = (Long)request.getAttribute("xjs_startTime");
//        long endTime = System.currentTimeMillis();
//        long executeTime = endTime - startTime;
//        //modified the exisitng modelAndView
//        //modelAndView.addObject("executeTime",executeTime);
//        response.addDateHeader("executeTime", executeTime);
//        request.setAttribute("executeTime", executeTime);
    }

    /**
     * 全部完成后处理
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        String tt= httpServletRequest.getRequestURL() + "->请求用时：" + (System.currentTimeMillis() - threadLocal.get());
//        //httpServletResponse.setCharacterEncoding("UTF-8");
//        httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
    }
}